package qlsl.androiddesign.view.subview.fragmentview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qlsl.androiddesign.demo.R;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import qlsl.androiddesign.activity.commonactivity.SearchActivity;
import qlsl.androiddesign.adapter.subadapter.PoemDynamicAdapter;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.PoemDynamic;
import qlsl.androiddesign.fragment.subfragment.SearchDynamicFragment;
import qlsl.androiddesign.http.service.commonservice.SearchService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshGridView;
import qlsl.androiddesign.listener.OnPullRefreshListener;
import qlsl.androiddesign.view.baseview.FunctionView;

public class SearchDynamicView extends FunctionView<SearchActivity> {

	private SearchDynamicFragment fragment;

	private PullToRefreshGridView refreshView;

	private List<PoemDynamic> list = new ArrayList<PoemDynamic>();

	private Pager pager;

	private String key;

	public SearchDynamicView(SearchDynamicFragment fragment, ViewGroup rootView, ViewGroup contentView) {
		super((SearchActivity) fragment.getActivity());
		this.fragment = fragment;
		setContentView(rootView, contentView);
	}

	protected void initView(View view) {
		hideTitleBar();
		refreshView = findViewById(R.id.refreshGridView);
	}

	protected void initData() {

	}

	protected void initListener() {
		refreshView.setOnRefreshListener(onRefreshListener);
	}

	@SuppressWarnings("unchecked")
	public <T> void showData(T... t) {
		if (t[0] instanceof Map) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			List<PoemDynamic> list_result = (List<PoemDynamic>) map.get("list");
			pager = (Pager) map.get("pager");
			key = (String) map.get("key");
			if (pager.getPageNo() == 0) {
				resetList(list_result);
			} else {
				list.addAll(list_result);
			}
			notifyDataSetChanged();
		}
	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {

	}

	private void search(int page) {
		SearchService.search(key, page, this, fragment);
	}

	private void resetList(List<PoemDynamic> list_result) {
		list.clear();
		list.addAll(list_result);
	}

	private void notifyDataSetChanged() {
		BaseAdapter adapter = refreshView.getBaseAdapter();
		if (adapter == null) {
			adapter = new PoemDynamicAdapter(activity, list);
			refreshView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
		refreshView.setReleaseLabel(pager.getPageNo(), list.size(), pager.getTotalPage(), pager.getTotalCount());
		refreshView.onRefreshComplete();
	}

	private OnPullRefreshListener<GridView> onRefreshListener = new OnPullRefreshListener<GridView>() {

		public void onPullDown() {
			search(0);
		}

		public void onPullUp() {
			super.onPullUp(refreshView, pager);
		}

		public void nextPager() {
			search(pager.getPageNo() + 1);
		}

	};

}
